﻿using DAL.Migrations;
using IDAL;
using Model;
using Model.ShowViewPayment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class PaymentDAL : IPaymentDAL
    {
        //添加病人支付订单
        public int AddPayment(Payment p)
        {
            using (MyDBContext db = new MyDBContext())
            {
                db.Payments.Add(p);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 绑定支付类型
        /// </summary>
        /// <returns></returns>

        public List<ChargeType> BangChargeType()
        {
            using (MyDBContext db=new MyDBContext())
            {
                var list = db.ChargeType.ToList();
                return list;
            }
        }

        /// <summary>
        /// 绑定优惠方式
        /// </summary>
        /// <returns></returns>

        public List<Discount> BangDiscount()
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Discount.ToList();
                return list;
            }
        }

        //绑定病人下拉
        /// <returns></returns>
        public List<Patient> BindPatient()
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Patient.ToList();
                return list;
            }
        }
        /// <summary>
        /// 根据选中的患者编号反填
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public List<FanTianView> FanTian(int fid)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = (from a in db.Payments
                            join b in db.Patient on a.Patient_Id equals b.Patient_Id
                            join c in db.Outpatient on a.Outpatient_Id equals c.Outpatient_Id
                            join d in db.Discount on a.Discount_Id equals d.Discount_Id
                            join e in db.Doctor on a.Doctor_Id equals e.Doctor_Id
                            join f in db.PatientInfor on a.PatientInfor_Id equals f.PatientInfor_Id
                            orderby b.Patient_Date
                            select new FanTianView
                            {
                                Patient_Id = b.Patient_Id,
                                Patient_Name = b.Patient_Name,
                                Doctor_Name = e.Doctor_Name,
                                Outpatient_Name = c.Outpatient_Name,
                                Patient_Date = b.Patient_Date,
                                PatientInfor_registration = f.PatientInfor_registration,
                            });
                if (fid != 0)
                {
                    list = list.Where(b => b.Patient_Id == fid);
                }
                return list.ToList();
            }
        }

        //支付查询
        public List<ShowPaymentView> GetPayment(string name)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = (from a in db.Payments
                            join b in db.Patient on a.Patient_Id equals b.Patient_Id
                            join c in db.Outpatient on a.Outpatient_Id equals c.Outpatient_Id
                            join d in db.Discount on a.Discount_Id equals d.Discount_Id
                            join e in db.Doctor on a.Doctor_Id equals e.Doctor_Id
                            select new ShowPaymentView
                            {
                                Payment_Id = a.Patient_Id,
                                Patient_Name = b.Patient_Name,
                                Doctor_Name = e.Doctor_Name,
                                Outpatient_Name = c.Outpatient_Name,
                                Payment_Sum = a.Payment_Sum,
                                Discount_Rate = d.Discount_Rate,
                                Payment_Actual = a.Payment_Actual,
                                Patient_State = b.Patient_State
                            });
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(b => b.Patient_Name.Contains(name));
                }
                return list.ToList();
            }
        }
    }
}
